//
//  NSString+StrExtension.m
//  test
//
//  Created by hanf on 15/4/22.
//  Copyright (c) 2015年 searainbow. All rights reserved.
//

#import "NSString+StrExtension.h"

@implementation NSString (StrExtension)

// 判断是不是可用的手机号码或固话
- (BOOL)isUserPhone
{
    NSString * MOBILE = @"^[1][3578]\\d{9}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
   
    return [regextestmobile evaluateWithObject:self];
}

// 判断用户名
- (BOOL)isUserName
{
    NSString *regex = @"^([\u4e00-\u9fa5]|[A-Za-z]){2,}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

// 判断密码
- (BOOL)isPassword
{
    NSString *regex = @"^[?!\\x00-\\xff]{6,10}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

// 判断公司名
- (BOOL)isCompany
{
    NSString *regex = @"^([\u4e00-\u9fa5]|[A-Za-z])+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

// 判断职务
- (BOOL)isPosition
{
    NSString *regex = @"^([\u4e00-\u9fa5]|[A-Za-z])+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

// 判断固话
- (BOOL)isFixTel
{
    NSString *regexThree = @"^(010|021|022|023|024|025|026|027|028|029|852)-\\d{8}$";
    NSString *regexFour = @"^(0[3-9][1-9]{2})-\\d{7,8}$";
    NSPredicate *predThree = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexThree];
    NSPredicate *predFour = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexFour];

    return [predThree evaluateWithObject:self] || [predFour evaluateWithObject:self];
}

// 判断是否是座机电话
- (BOOL)isFixTelWithOutConnection {
    NSString *regexThree = @"^(010|021|022|023|024|025|026|027|028|029|852)\\d{8}$";
    NSString *regexFour = @"^(0[3-9][0-9]{2})\\d{7,8}$";
    NSPredicate *predThree = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexThree];
    NSPredicate *predFour = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexFour];
    
    return [predThree evaluateWithObject:self] || [predFour evaluateWithObject:self];
}

// 判断邮箱
- (BOOL)isEmail
{
    NSString *email = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *regextestEmail = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", email];
    
    return [regextestEmail evaluateWithObject:self];
}
// 判断是否为空
- (BOOL)checkInputMsg {

    NSString *predicateStr = @"^[\\s]{0,}$";//[0-9]*digital
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", predicateStr];
    
    return [predicate evaluateWithObject:self];
}
// 判断输入信息是否为数字
- (BOOL)checkInputDigital {
    
    NSString *digitalStr = @"[0-9]*";//[0-9]*digital
    NSPredicate *digital = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", digitalStr];
    
    return [digital evaluateWithObject:self];
}
// 判断是否含有表情
- (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    
    return returnValue;
}

- (BOOL)disable_emoji:(NSString *)text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@"!@#¥%*"];
    
    NSRange range;
    
    range = [modifiedString rangeOfString:@"!@#¥%*"];
    if (range.location != NSNotFound) {
        return YES;
    }else{
        return NO;
    }
}

@end
